home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / evalexp.c < prev    next >
C/C++ Source or Header  |  1990-07-28  |  10KB  |  265 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. #include "sim.h"
  25. #include "aux.h"
  26.  
  27. extern double floatval();
  28. extern LONG   makefloat();
  29.  
  30. /***********************************************************************/
  31. static eval_error(functor, arity)
  32. CHAR_PTR functor;
  33. WORD     arity;
  34. {
  35.    printf("Error: unknown functor %s/%d in arithmetic expression\n",
  36.           functor, arity);
  37. }
  38.  
  39. /***********************************************************************/
  40. eval(op, val)
  41. LONG     op;
  42. LONG_PTR val;
  43. {
  44.    register LONG_PTR top;
  45.    PSC_REC_PTR       psc_ptr;
  46.    CHAR_PTR          functor;
  47.    WORD              arity, floatp;
  48.    LONG              length, op1, op2;
  49.    double            result;
  50.  
  51.    switch (TAG(op)) {
  52.       case FREE:
  53.          printf("Error: unbound variable in arithmetic expression\n");
  54.          FAIL0;
  55.          return -1;
  56.  
  57.       case CS:
  58.          floatp = 0;
  59.          psc_ptr = GET_STR_PSC(op);
  60.          functor = GET_NAME(psc_ptr);
  61.          arity   = GET_ARITY(psc_ptr);
  62.          length  = GET_LENGTH(psc_ptr);
  63.          if (length == 1) {
  64.             switch (*functor) {
  65.                case '+':
  66.                   if (arity == 1) {
  67.              op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  68.                      return eval(op1, val);
  69.                   } else if (arity == 2) {
  70.              op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  71.                      floatp = eval(op1, &op1);
  72.                      op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  73.                      floatp = floatp | eval(op2, &op2);
  74.                      if (floatp >= 0)
  75.                         *val = MAKENUM(NUMVAL(op1) + NUMVAL(op2));
  76.                      return floatp;
  77.                   } else {
  78.              eval_error(functor, arity);
  79.                      FAIL0;
  80.                      return -1;
  81.                   }
  82.                case '-':
  83.           if (arity == 1) {
  84.                      op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  85.                      floatp = eval(op1, &op1);
  86.                      if (floatp >= 0) {
  87.                         *val = MAKENUM(-(NUMVAL(op1)));
  88.                         return floatp;
  89.                      }
  90.                   } else if (arity == 2) {
  91.              op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  92.                      floatp = eval(op1, &op1);
  93.                      op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  94.                      floatp = floatp | eval(op2, &op2);
  95.                      if (floatp >= 0)
  96.                         *val = MAKENUM(NUMVAL(op1) - NUMVAL(op2));
  97.                      return floatp;
  98.                   } else {
  99.              eval_error(functor, arity);
  100.                      FAIL0;
  101.                      return -1;
  102.                   }
  103.                case '*':
  104.                   if (arity == 2) {
  105.                      op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  106.                      floatp = eval(op1, &op1);
  107.                      op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  108.                      floatp = floatp | eval(op2, &op2);
  109.                      if (floatp >= 0)
  110.                         *val = MAKENUM(NUMVAL(op1) * NUMVAL(op2));
  111.                      return floatp;
  112.                   } else {
  113.                      eval_error(functor, arity);
  114.                      FAIL0;
  115.                      return -1;
  116.                   }
  117.                case '/':
  118.                   if (arity == 2) {
  119.                      op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  120.                      floatp = eval(op1, &op1);
  121.                      op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  122.                      floatp = floatp | eval(op2, &op2);
  123.                      if (NUMVAL(op2) == 0) {
  124.                         printf(
  125.               "div: division by zero! ... failing execution ...\n");
  126.                         return -1;
  127.                      }
  128.                      if (floatp >= 0) {
  129.                         result = NUMVAL(op1) / NUMVAL(op2);
  130.                         *val = makefloat((double)result);
  131.                         return 1;
  132.                      }
  133.                   } else {
  134.                      eval_error(functor, arity);
  135.                      FAIL0;
  136.                      return -1;
  137.                   }
  138.               case '\\':
  139.                   if (arity == 1) {
  140.                      op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  141.                      floatp = eval(op1, &op1);
  142.                      if (floatp == 0) {
  143.                         *val = MAKEINT(~(INTVAL(op1)));
  144.                         return floatp;
  145.                      } else {
  146.                         printf("neg: integer required\n");
  147.                         return -1;
  148.                      }
  149.                   } else {
  150.                      eval_error(functor, arity);
  151.                      FAIL0;
  152.                      return -1;
  153.                   }
  154.             }  /* end of switch on (*functor) */
  155.      }  /* end of if (length == 1) */
  156.  
  157.          else if (arity == 2) {
  158.             if (!strcmp(functor, "//")) {
  159.                op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  160.                floatp = eval(op1, &op1);
  161.                op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  162.                floatp = floatp | eval(op2, &op2);
  163.                if (floatp >= 0)
  164.                   *val = MAKEINT(((NUMVAL(op1) / NUMVAL(op2))));
  165.                return 0;
  166.             }
  167.             else if (!strcmp(functor, "mod")) {
  168.                op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  169.                floatp = eval(op1, &op1);
  170.                op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  171.                floatp = floatp | eval(op2, &op2);
  172.                if (floatp == 0) {
  173.                   *val = MAKEINT(INTVAL(op1) % INTVAL(op2));
  174.                   return 0;
  175.                } else {
  176.                   printf("mod: integer required\n");
  177.                   return -1;
  178.                }
  179.             }
  180.             else if (!strcmp(functor, "/\\")) {
  181.                op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  182.                floatp = eval(op1, &op1);
  183.                op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  184.                floatp = floatp | eval(op2, &op2);
  185.                if (floatp == 0) {
  186.                   *val = MAKEINT(INTVAL(op1) & INTVAL(op2));
  187.                   return 0;
  188.                } else {
  189.                   printf("and: integer required\n");
  190.                   FAIL0;
  191.                   return -1;
  192.                }
  193.             }
  194.             else if (!strcmp(functor, "\\/")) {
  195.                op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  196.                floatp = eval(op1, &op1);
  197.                op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  198.                floatp = floatp | eval(op2, &op2);
  199.                if (floatp == 0) {
  200.                   *val = MAKEINT(INTVAL(op1) | INTVAL(op2));
  201.                   return 0;
  202.                } else {
  203.                   printf("or: integer required\n");
  204.                   FAIL0;
  205.                   return -1;
  206.                }
  207.             }
  208.             else if (!strcmp(functor, ">>")) {
  209.                op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  210.                floatp = eval(op1, &op1);
  211.                op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  212.                floatp = floatp | eval(op2, &op2);
  213.                if (floatp == 0) {
  214.                    *val = MAKEINT(INTVAL(op1) >> INTVAL(op2));
  215.                    return 0;
  216.                } else {
  217.                   printf("rshift: integer required\n");
  218.                   FAIL0;
  219.                   return -1;
  220.                }
  221.             }
  222.             else if (!strcmp(functor, "<<")) {
  223.                op1 = *(((LONG_PTR)(UNTAG(op))) + 1);  DEREF(op1);
  224.                floatp = eval(op1, &op1);
  225.                op2 = *(((LONG_PTR)(UNTAG(op))) + 2);  DEREF(op2);
  226.                floatp = floatp | eval(op2, &op2);
  227.                if (floatp == 0) {
  228.                   *val = MAKEINT(INTVAL(op1) << INTVAL(op2));
  229.                   return 0;
  230.                } else {
  231.                   printf("lshift: integer required\n");
  232.                   FAIL0;
  233.                   return -1;
  234.                }
  235.             }
  236.             else {
  237.                eval_error(functor, arity);
  238.                FAIL0;
  239.                return -1;
  240.             }
  241.          }
  242.          else {
  243.             eval_error(functor, arity);
  244.             FAIL0;
  245.             return -1;
  246.          }  /* end of if (arity == 2) */
  247.       /* end of CS case */
  248.  
  249.       case NUM:
  250.          *val = op;
  251.          return ISFLOAT(op);
  252.  
  253.       case LIST:
  254.          UNTAG(op);
  255.      if (ISNUM(*(LONG_PTR)op) && *(LONG_PTR)(op+4) == nil_sym) {
  256.        *val = op;     /* convert [N] to N, N is a number */
  257.        return ISFLOAT(*(LONG_PTR)op);
  258.      }
  259.      else {
  260.        FAIL0;
  261.            return -1;
  262.     }
  263.    }
  264. }
  265.